<?php
//print_r($_POST);
$_POST["uid"] = $_SESSION['uid'];
//echo $_SESSION['uid'].'mff';
function getFromDB()
{
$_SESSION['timeline'] = $_SESSION['timeline'] . 'gfdb';

//echo 'hellogetfromdb';
	db_connect();
	$query = 'select * from companies where id ='.$_SESSION['userID'];
	addDebug('querytext:'.$query);
	$record = getRecords($query);
	//echo count($record);
	addDebug(count($record));
	$_SESSION["id"] = $record["id"];
	$_SESSION["name"] = $record["name"];
	addDebug($_SESSION["name"].' name');
	$_SESSION["debug"] = $_SESSION["debug"] . $_SESSION["name"].' name value <br>';
	$_SESSION["website"] = $record["website"];
	$_SESSION["first_name"] = $record["first_name"];
	$_SESSION["last_name"] = $record["last_name"];
	$_SESSION["address"] = $record["address"];
	$_SESSION["city"] = $record["city"];
	$_SESSION["state"] = $record["state"];
	$_SESSION["zip"] = $record["zip"];
	$_SESSION["office_phone_number"] = $record["office_phone_number"];
	$_SESSION["toll_free_number"] = $record["toll_free_number"];
	$_SESSION["fax_number"] = $record["fax_number"];
	$_SESSION["email"] = $record["email"];
	$_SESSION["airport_one"] = $record["airport_one"];
	$_SESSION["airport_two"] = $record["airport_two"];
	$_SESSION["airport_three"] = $record["airport_three"];
	$_SESSION["airport_four"] = $record["airport_four"];
	$_SESSION["airport_five"] = $record["airport_five"];
	$_SESSION["airport_six"] = $record["airport_six"];
	$_SESSION["airport_seven"] = $record["airport_seven"];
	$_SESSION["airport_eight"] = $record["airport_eight"];
	$_SESSION["target_area_one"] = $record["target_area_one"];
	$_SESSION["target_area_two"] = $record["target_area_two"];
	$_SESSION["target_area_three"] = $record["target_area_three"];
	$_SESSION["target_area_four"] = $record["target_area_four"];
	$_SESSION["dt_changed"] = $record["dt_changed"];
	$_SESSION["airtrans"] = $record["airtrans"];
	$_SESSION["weddings"] = $record["weddings"];
	$_SESSION["prom"] = $record["prom"];
	$_SESSION["bachelor"] = $record["bachelor_party"];
	$_SESSION["anniversaries"] = $record["anniversaries"];
	$_SESSION["pt2pt"] = $record["point2point"];
	$_SESSION["birthday"] = $record["birthday_party"];
	$_SESSION["concert"] = $record["concert"];
	$_SESSION["grads"] = $record["graduation"];
	$_SESSION["sports"] = $record["sport_event"];
	$_SESSION["holiday"] = $record["holiday_lights_tour"];
	$_SESSION["nightout"] = $record["night_on_the_town"];
	$_SESSION["other"] = $record["other"];
	$_SESSION["sedan"] = $record["sedan"];
	$_SESSION["limo"] = $record["limo"];
	$_SESSION["stretchlimo"] = $record["strech_limo"];
	$_SESSION["van"] = $record["van"];
	$_SESSION["paratrans"] = $record["paratrans_van"];
	$_SESSION["handicap"] = $record["handicap_access"];
	$_SESSION["suv"] = $record["suv"];
	$_SESSION["minibus"] = $record["minibus"];
	$_SESSION["antique"] = $record["antique_vehicle"];
	$_SESSION["rolls"] = $record["rolls_royce"];
	$_SESSION["bus620"] = $record["partybus_620"];
	$_SESSION["bus20up"] = $record["partybus_20plus"];
	if (is_array($record))
	{
	foreach($record as $cell)
			addDebug('cell :'.$cell);		
			}
	getAirportsFromDB();
}
function isServiceSelected($option)
{
	//echo '<br> iss - ' . $option . '<br>';
	if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_GET["mode"]))
	{
	addDebug('iss method - post');
		@$service=$_POST['service'];
		if(is_array($service))
		{
			while (list ($key, $val) = each($service))
			{
				if ($val == $option) 
				{
					echo 'selected';
					addDebug('selected: '.$key.' - '.$val);
				}
			}
		}	
	}
	else
	{
		addDebug('iss method - get');
		if (isset($_SESSION[$option]))
		{
			if ($_SESSION[$option] == 'Y')
			{
				echo 'selected';
				addDebug2($option . ' is selected');
			}
			else
				echo '';
			addDebug2($option.' iss option : '.$_SESSION[$option]);
		}
		else
			echo '';
	}
}

function isServiceSelectedYN($option)
{
try {
@$airports=$_POST['#asmContainer1'];
if(is_array($airports))
addDebug2('airportsisarray');
addDebug2('issyn option: '.$option);
	$retval = 'U';
	@$service=$_POST['asmSelect0'];
	print_r($asmSelect);
	if(is_array($service))
	{
	adddebug2('service is array');
		$retval = 'N';
		foreach ($service as $val)
		{
		adddebug2('issyn val: '.$val);
			if ($val == $option) $retval = 'Y';
			//echo('<br>$val' . $val . ' $option' . $option . ' $retval ' . $retval . '<br>');
		}		
	}
	else
	{
		adddebug2('service isnt array '.$service[0]);
	}
	return $retval;
	}
	catch (Exception $e) 
	{
		echo 'Caught exception: ',  var_dump($e->getMessage()), "\n";
	}	
} 
function isAirportSelected2($option)
{
addDebug('option: '.$option);
	$retval = 'U';
	@$airports=$_POST['airports'];
	if(is_array($airports))
	{
		$retval = 'N';
		while (list ($key, $val) = each($airports))
		{
		addDebug('val: '.$val);
			if ($val == $option) $retval = 'Y';
			////echo('<br>$val' . $val . ' $option' . $option . ' $retval ' . $retval . '<br>');
		}		
	}
	return $retval;
} 
function isFleetSelectedYN($option)
{
addDebug('fleetynoption: '.$option);
	//echo '<br> iss - ' . $option . '<br>';
	@$fleet=$_POST['fleet'];
	if(is_array($fleet))
	{
		$retval = 'N';
		foreach ($fleet as $val)
		{
		addDebug('val: '.$val);
			if ($val == $option) $retval = 'Y';
			////echo('<br>$val' . $val . ' $option' . $option . ' $retval ' . $retval . '<br>');
		}		
	}
	else
		addDebug2('fleet is not array');

	////echo("<h2>$retval</h2>");
	return $retval;
} 
function isFleetSelected($option)
{
	@$fleeT=$_POST['FLEET'];	
	IF ($_SERVER['REQUEST_METHOD'] == 'POST')
	{
		IF(IS_ARRAY($FLEET))
		{
			WHILE (LIST ($KEY, $VAL) = EACH($FLEET))
			{
				IF ($VAL == $OPTION) ECHO 'SELECTED';
			}
		}
	}	
	ELSE
	{
		ADDDEBUG('IFS METHOD - GET');
		if (isset($_SESSION[$option]))
		{
			if ($_SESSION[$option] == 'Y')
			{
				echo 'selected';
				addDebug($option . ' is selected');
			}
			else
				echo '';
			addDebug($option.'fmethodnotpost : '.$_SESSION[$option]);
		}
		else
			echo '';
	}	
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') //
{
try {
$id = $_SESSION['userID'];
//$c = $_POST['service'];
db_connect();
writeAirports();
	//echo('<br> postback');
	$name = (isTextSet('name','Company'));
	addDebug($name.'companyname');
	$website = (isTextSet('website','Web Address'));
	$firstname = (isTextSet('first_name','Web Address'));
	$lastname = (isTextSet('last_name','Last Name'));
	$address = (isTextSet('address','Address'));
	$city = (isTextSet('city','City'));
	$state = (isTextSet('state','State'));
	$zip = (isTextSet('zip','Zip code'));
	$officenum = (isTextSet('office_phone_number','Office Number'));	
	$tollfree = (isTextSet('toll_free_number','Toll free Number'));
	$fax = (isTextSet('fax_number','Fax'));
	$email = (isTextSet('email','Email'));	
	$airport1 = (isTextSet('airport_one','1st airport'));	
	$airport2 = (isTextSet('airport2','2nd airport'));
	$airport3 = (isTextSet('airport3','3rd airport'));
	$airport4 = (isTextSet('airport4','4th airport'));
	$airport5 = (isTextSet('airport5','5th airport'));	
	$airport6 = (isTextSet('airport6','6th airport'));
	$airport7 = (isTextSet('airport7','7th airport'));
	$airport8 = (isTextSet('airport8','8th airport'));
	$targetzip1 = (isTextSet('targetzip1','Target Zipcode'));	
	$targetzip2 = (isTextSet('targetzip2','Target Zipcode'));
	$targetzip3 = (isTextSet('targetzip3','Target Zipcode'));
	$targetzip4 = (isTextSet('targetzip4','Target Zipcode'));	
	$airtrans = 'N';
	if (isServiceSelected('airtrans') == 'selected') $airtrans = 'Y';
	$wedding = isServiceSelectedYN('wedding');
	echom('<br>after wedding<br>');
	$prom = isServiceSelectedYN('prom');
	$bachelor = isServiceSelectedYN('bachelor');
	$anniversaries = isServiceSelectedYN('anniversaries');
	$pt2pt = isServiceSelectedYN('pt2pt');
	$birthday = isServiceSelectedYN('birthday');
	$concert = isServiceSelectedYN('concert');
	$grads = isServiceSelectedYN('grads');
	$sports = isServiceSelectedYN('sports');
	$holiday = isServiceSelectedYN('holiday');
	$nightout = isServiceSelectedYN('nightout');
	$other = isServiceSelectedYN('other');
	
	//fleet selections
	$sedan = isFleetSelectedYN('sedan');
	$stretchlimo = isFleetSelectedYN('stretchlimo');
	$van = isFleetSelectedYN('van');
	$paratrans = isFleetSelectedYN('paratrans');
	$handicap = isFleetSelectedYN('handicap');
	$suv = isFleetSelectedYN('suv');
	$minibus = isFleetSelectedYN('minibus');
	$limo = isFleetSelectedYN('limo');
	$antique = isFleetSelectedYN('antique');
	$rolls = isFleetSelectedYN('rolls');
	$bus620 = isFleetSelectedYN('bus620');
	$bus20up = isFleetSelectedYN('bus20up');
				//echo '456';
	//testcodellllllllllllll
	$del = 'delete from companies where id=' . $_SESSION["userID"] . ';';
	
	$sql = "insert into companies (
	id,
	name,
	website,
	first_name,
	last_name,
	address,
	city,
	state,
	zip,
	office_phone_number,
	toll_free_number,
	fax_number,
	email,
	airport_one,
	airport_two,
	airport_three,
	airport_four,
	airport_five,	
	airport_six,
	airport_seven,
	airport_eight,	
	target_area_one,
	target_area_two,
	target_area_three,
	target_area_four,
	dt_changed,
	airtrans,	
	weddings,
	prom,
	bachelor_party,
	anniversaries,
	point2point,
	birthday_party,
	concert,
	graduation,
	sport_event,
	holiday_lights_tour,
	night_on_the_town,
	other,
	sedan,
	limo,
	strech_limo,
	van,
	paratrans_van,
	handicap_access,
	suv,
	minibus,
	antique_vehicle,
	rolls_royce,
	partybus_620,
	partybus_20plus) values (
	'$id',
	'$name',
	'$website',
	'$firstname',
	'$lastname',
	'$address',
	'$city',
	'$state',
	'$zip',
	'$officenum',
	'$tollfree',
	'$fax',
	'$email',
	'$airport1',
	'$airport2',
	'$airport3',
	'$airport4',
	'$airport5',
	'$airport6',
	'$airport7',
	'$airport8',	
	'$targetzip1',
	'$targetzip2',
	'$targetzip3',
	'$targetzip4',
	'Now()',
	'$airtrans',
	'$wedding',
	'$prom',
	'$bachelor',
	'$anniversaries',
	'$pt2pt',
	'$birthday',
	'$concert',
	'$grads',
	'$sports',
	'$holiday',
	'$nightout',
	'$other',
	'$sedan',
	'$limo',
	'$stretchlimo',
	'$van',
	'$paratrans',
	'$handicap',
	'$suv',
	'$minibus',
	'$antique',
	'$rolls',
	'$bus620',
	'$bus20up')";
	//echo '<h2>here</h2>';
	
	}
	catch (Exception $e) 
	{
		echo 'Caught exception: ',  var_dump($e->getMessage()), "\n";
	}

	runNonQuery($del);
	runNonQuery($sql);
	

//echo '<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>';
//phpinfo();
//echo '<br> last error:';
//print_r(error_get_last());
}
else //on pageload
{
	//echo '<br> not postback';
	
}
getFromDB();
function isTextSet($control,$label)
{
$_SESSION['timeline'] = $_SESSION['timeline'] . 'its';
adddebug('ITS ctl: ' . $_SESSION[$control]);
//echo $label;
//echo $control;
//echo $_SERVER['REQUEST_METHOD'];
	$return = 'error';
	if ($_SERVER['REQUEST_METHOD'] == 'POST')
	{	
		$return = $_POST[$control]; //was $field
		// addDebug('$field is: '.$field);
		// if ($field) 
		// {
			// if ($field != $label)
				// $return = $field;
			// else
				// $return = $label;
		// }
		// else
			// $return = $label;
	}
	else
	{
	//echo 'ctlval'.isset($_SESSION[$control]);	
		$return = $_SESSION[$control];
	}
	addDebug('$return is: '.$return);
	return $return;
}
function runNonQuery($sql)
{
	try 
	{
			//echo '<h2>test</h2>';
		include('dbcon.php');
		if (!$con)
		{
			die('Could not connect: ' . mysql_error());
		}
		addDebug3('about to insert'.$sql);
		mysql_select_db(MYSQLDB, $con);
		//echo($sql);
		// Execute query
		$r = mysql_query($sql,$con) or die ('query error, memberFormFunctions:runNonQuery ' . mysql_error());
		//if ($r == 'false') 
			//echo('query error, memberFormFunctions:runNonQuery ' . mysql_error());
		addDebug3('<h2> query result: ' . ($r ? 'true' : 'false') . '</h2>');
		addDebug2('sqlerr' . mysql_error()); 
		mysql_close($con);

		//echo('inserted records');
	}
	catch (Exception $e) 
	{
		echo 'Caught exception: ',  var_dump($e->getMessage()), "\n";
	}		
}
function getRecords($sql)
{
	try 
	{
			//echo '<h2>test</h2>';
		include('dbcon.php');
		if (!$con)
		{
			die('Could not connect: ' . mysql_error());
		}
		addDebug('about to insert');
		mysql_select_db(MYSQLDB, $con);
		adddebug($sql);
		//echo '<br>query text:'.$sql.'<br>';
		// Execute query
		$r = mysql_query($sql,$con) or die('query error, memberFormFunctions:getRecords'.mysql_error());
		if ($row = mysql_fetch_assoc($r))
			return $row;
		mysql_close($con);

		//echo('inserted records');
	}
	catch (Exception $e) 
	{
		echo 'Caught exception: ',  var_dump($e->getMessage()), "\n";
	}	
}
function echom($message)
{
	//echo '<br>' . $message . '<br>';
}
function currentDate()
{
	$my_t=getdate(date("U"));
	return ("$my_t[weekday], $my_t[month] $my_t[mday], $my_t[year] - $my_t[hours]:$my_t[minutes]:$my_t[seconds]");	
}
function currentTime()
{
	$my_t=getdate(date("U"));
	return ("$my_t[hours]:$my_t[minutes]:$my_t[seconds]");
}
function isAirportSelected($airportid)
{
//addDebug2('in ias' .$airportid);
//addDebug2('airportlistcount ' . count($_SESSION['airportlist']));
		if(is_array($_SESSION['airportlist']))
		{
		//addDebug2('in array');
			foreach ($_SESSION['airportlist'] as $value)
			{
				//addDebug2('in foreach loop');
				//addDebug2("isAirportSelected ".$value. " lookup: ".$airportid);
				if ($value == $airportid)
				{
					echo "selected";
					//addDebug2('match ' .$value. ' - ' .$airportid);
				}
			}
		}	
}
function writeAirports()
{		
		@$airports=$_POST['airports'];
		$insert = 'insert into companyairports (airportid, companyid) values ';
		if(is_array($airports))
		{
			while (list ($key, $val) = each($airports))
			{
			addDebug("writeairports ".$val);
					$insertvalues = $insertvalues . "(" . $val. ", ". $_SESSION['userID']. "),";
			}
			$insert = $insert . substr($insertvalues,0,strlen($insertvalues) - 1);
			//echo $insert;
			runNonQuery("delete from companyairports where companyid =".$_SESSION['userID']);
			runNonQuery($insert);
		}	
}
function getAirportsFromDB()
{
//addDebug2('in getairports');
	unset($_SESSION['airportlist']);
	//$_SESSION['airportlist'] = array(0);
	include('dbcon.php');
	if (!$con)
	{
		die('Could not connect: ' . mysql_error());
	}
	mysql_select_db(MYSQLDB, $con);
	$qry = "SELECT * FROM companyairports where companyid =".$_SESSION['userID'];
	$result = mysql_query($qry);
//echo $result;
addDebug("<h2>querycompair".$qry."</h2>");
	while($row = mysql_fetch_array($result))
		{
			//adddebug2('airportissel' . $row["airportid"]);
			$_SESSION['airportlist'][] = $row["airportid"];
			// if ($row["airportid"] == $value)
				// echo "selected";		
		}	
	mysql_close($con);
}
function addDebug($msg)
{
//echo 'hello'.$msg;
	//$_SESSION['debug'] = $_SESSION['debug'] . '<br>'.currentTime().':    '.$msg.'<br>';
//echo $_SESSION['debug'].'<br>debugvalue<br>';
}
function addDebug2($msg)
{
//echo 'hello'.$msg;
	//$_SESSION['debug'] = $_SESSION['debug'] . '<br>'.currentTime().':    '.$msg.'<br>';
//echo $_SESSION['debug'].'<br>debugvalue<br>';
}
function addDebug3($msg)
{
//echo 'hello'.$msg;
	$_SESSION['debug'] = $_SESSION['debug'] . '<br>'.currentTime().':    '.$msg.'<br>';
//echo $_SESSION['debug'].'<br>debugvalue<br>';
}
//echo '-----'.$_SESSION['timeline'].'timeline value';

?>
